﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Util.Helper
{
    public static class HashHelper
    {

        public static string GetPsw(string psw, string salt)
        {

            if (string.IsNullOrEmpty(psw)) { return psw; }

            using var md5 = System.Security.Cryptography.MD5.Create();

            var result = md5.ComputeHash(Encoding.UTF8.GetBytes(psw));
            var strResult = BitConverter.ToString(result);

            var result2 = md5.ComputeHash(Encoding.UTF8.GetBytes(salt));
            var strResult2 = BitConverter.ToString(result);

            var strResult_a = strResult.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var strResult2_a = strResult2.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

            string temp = strResult2_a[strResult2_a.Length - 1];
            char tempc = temp[temp.Length - 1];

            for (int i = 0; i < strResult2_a.Length; i++)
            {

                if (tempc % 2 == 0)
                {
                    if (i % 2 == 0)
                    {
                        strResult_a.Insert(i, strResult2_a[i]);
                    }
                }
                else
                {
                    if (i % 2 > 0)
                    {
                        strResult_a.Insert(i, strResult2_a[i]);
                    }
                }
            }

            result = md5.ComputeHash(Encoding.UTF8.GetBytes(string.Join("", strResult_a)));
            strResult = BitConverter.ToString(result);
            strResult = strResult.Replace("-", "");

            return strResult;
        }

    }
}
